package model

import (
	"biyesheji/utils/errmsg"
	"biyesheji/utils/tools"
	"sort"
	"strings"

	"gorm.io/gorm"
)

type Sympthom struct {
	gorm.Model
	Description  string `gorm:"type:varchar(255);" json:"description"`
	Department   string `gorm:"type:varchar(500);" json:"department"`
	Sympthomname string `gorm:"type:varchar(255);" json:"sympthomname"`
	Keyword      string `gorm:"type:varchar(255);" json:"keyword"`
	Medicine     string `gorm:"type:varchar(255);" json:"medicine"`
	Part         string `gorm:"type:varchar(255);" json:"part"`
	Pid          uint   `gorm:"primary_key;auto_increment" json:"pid"`
}

type Part struct {
	gorm.Model
	Children []Sympthom `gorm:"foreignkey:Pid"`
	Part     string     `gorm:"type:varchar(255);" json:"part"`
	Pid      uint       `gorm:"type:int;not null" json:"pid"`
}

type APISPINFO struct {
	// 获取详情等信息而定义的结构体
	Sympthomname string `gorm:"type:varchar(255);" json:"sympthomname"`
	Description  string `gorm:"type:varchar(255);" json:"description"`
	Department   string `gorm:"type:varchar(500);" json:"department"`
	Medicine     string `gorm:"type:varchar(255);" json:"medicine"`
}

// 查询所有部位及症状信息
func GetAllSympthom(pageSize int, pageNum int) ([]Sympthom, int64) {
	var sympthom []Sympthom
	var total int64 // 在gorm中，total为int64
	err := db.Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&sympthom).Error
	db.Model(&Sympthom{}).Count(&total)
	if err != nil {
		return nil, 0
	}
	return sympthom, total
}

// 查询身体部位
func GetPart() interface{} {
	var part []string
	err := db.Model(&Part{}).Pluck("part", &part).Error
	if err != nil {
		return nil
	}
	return part
}

// 查询各个部位的症状关键字
func GetKeyword(part string) (interface{}, int) {
	var keyword []string
	var array []string
	err := db.Model(&Sympthom{}).Where("part LIKE ?", "%"+part+"%").Pluck("keyword", &keyword).Error
	if err != nil {
		return nil, errmsg.ERROR_KEYWORD_NULL
	}
	for _, item1 := range keyword {
		splitStr := strings.Split(item1, "、")
		for _, item2 := range splitStr {
			array = append(array, item2)
		}
	}
	sort.Strings(array)
	array = tools.RemoveDuplicatesAndEmpty(array)
	return array, errmsg.SUCCESS
}

// 查询症状名称
func GetSympthomName(part string, keyword string) (interface{}, int) {
	var name []string
	err := db.Model(&Sympthom{}).Where("part LIKE ? and keyword LIKE ?", "%"+part+"%", "%"+keyword+"%").Pluck("sympthomname", &name).Error
	if err != nil {
		return nil, errmsg.ERROR_SYMPTHOMNAME_NULL
	}
	return name, errmsg.SUCCESS
}

// 进一步查询关键字
func GetMoreKeyword(part string, keyword string) ([]string, int) {
	var morekeyword []string
	var array []string
	err := db.Model(&Sympthom{}).Where("part LIKE ? and keyword LIKE ?", "%"+part+"%", "%"+keyword+"%").Pluck("keyword", &morekeyword).Error
	if err != nil {
		return nil, errmsg.ERROR_KEYWORD_NULL
	}
	for _, item1 := range morekeyword {
		splitStr := strings.Split(item1, "、")
		for _, item2 := range splitStr {
			array = append(array, item2)
		}
	}
	sort.Strings(array)
	array = tools.RemoveDuplicatesAndEmpty(array)
	return array, errmsg.SUCCESS
}

// 查询科室，详情，推荐用药
func GetSomeInfo(sympthomname string) (interface{}, int) {
	var info APISPINFO
	err := db.Model(&Sympthom{}).Where("sympthomname = ?", sympthomname).Find(&info).Error
	if err != nil {
		return nil, errmsg.ERROR_SYMPTHOMINFO_NULL
	}
	return info, errmsg.SUCCESS
}

// 修改症状列表的数据
func EditSympthom(id int, data *Sympthom) int {
	var editSymp *Sympthom
	var maps = make(map[string]interface{})
	maps["description"] = data.Description
	maps["department"] = data.Department
	maps["sympthomname"] = data.Sympthomname
	maps["keyword"] = data.Keyword
	maps["medicine"] = data.Medicine
	// maps["part"] = data.Part
	// maps["pid"] = data.Pid 外键不能修改
	err := db.Model(&editSymp).Where("id = ?", id).Updates(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// 模糊查询所有症状
func GetLikeSympthom(data string) ([]Sympthom, int) {
	var likeSympt []Sympthom
	err := db.Where("description LIKE ? or department LIKE ? or sympthomname LIKE ? or keyword LIKE ? or medicine LIKE ? or part LIKE ?", "%"+data+"%", "%"+data+"%", "%"+data+"%", "%"+data+"%", "%"+data+"%", "%"+data+"%").Find(&likeSympt).Error
	if err != nil {
		return nil, errmsg.ERROR_SYMPTHOMINFO_NULL
	}
	return likeSympt, errmsg.SUCCESS
}

// 新增症状信息(未做查重处理，有时间再来写)
func AddSympthom(data *Sympthom) int {
	err := db.Create(&data).Error
	if err != nil {
		return errmsg.ERROR // 500
	}
	return errmsg.SUCCESS
}
